<template>
  <div class="login">
    <div class="box">
      <el-form ref="from" :model="from" :rules="rules">
        <el-form-item>
          <div style="text-align: center">
            <img src="@/assets/common/login-logo.png" alt="">
          </div>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input
            v-model="from.mobile"
            prefix-icon="el-icon-user-solid"
            @keyup.enter.native="submit"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="from.password"
            prefix-icon="el-icon-lock"
            type="password"
            @keyup.enter.native="submit"
          />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      from: {
        mobile: '13800000002',
        password: '123456'
      },
      // 定义表单验证
      rules: {
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 6,
            max: 12,
            message: '请输入六到十二位的密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  computed: {},
  created() {},
  methods: {
    submit() {
      // 全局表单验证
      this.$refs.from.validate(async(result) => {
        if (result) {
          // 表单验证成功后发请求
          // const res = await sysLogin(this.from)
          // 将token存到cookie
          // Cookiejs.set('token', res.data)
          await this.$store.dispatch('user/toLogin', this.from)
          // 获取地址值将主页传过来的地址保存 当下次登录时直接跳转到当时的页面
          // 没有参数则默认为主页
          const _redirect = this.$route.query.redirect || '/'
          this.$router.push(_redirect)
          // console.log(res)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/common/login.jpg) no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 550px;
  }
}
</style>
